﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Restful.Entities;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.ServiceModel.Activation;
using System.Messaging;
namespace Restful.DataAccess 
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CitasAdmin : ICitasAdmin
    {
        public citaBE datosCita(string CODCIT)
        {
            try
            {
                citaBE objeto;
                using (SqlConnection oCn = new SqlConnection(ConfigurationManager.ConnectionStrings["CNX"].ConnectionString.ToString()))
                {
                    using (SqlCommand oCmd = new SqlCommand("USP_BUSCAR_CITA_BY_ID", oCn))
                    {
                        oCmd.Parameters.Add("@CODCIT", SqlDbType.VarChar, 20).Value = CODCIT;
                        oCmd.CommandType = CommandType.StoredProcedure;
                        oCmd.Connection = oCn;
                        oCn.Open();
                        SqlDataReader reader = oCmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            reader.Read();
                            objeto = new citaBE();
                            objeto.CODCIT = reader.GetInt32(reader.GetOrdinal("CODCIT"));
                            objeto.NOMPAC = reader.GetString(reader.GetOrdinal("NOMPAC"));
                            objeto.APEPPAC = reader.GetString(reader.GetOrdinal("APEPPAC"));
                            objeto.APEMPAC = reader.GetString(reader.GetOrdinal("APEMPAC"));
                            objeto.DNIPAC = reader.GetString(reader.GetOrdinal("DNIPAC"));
                            objeto.CORPAC = reader.GetString(reader.GetOrdinal("CORPAC"));
                            objeto.NOMEMP = reader.GetString(reader.GetOrdinal("NOMEMP"));
                            objeto.NOMOCU = reader.GetString(reader.GetOrdinal("NOMOCU"));
                            objeto.FECCITA = reader.GetString(reader.GetOrdinal("FECCITA"));
                            objeto.DESHOR = reader.GetString(reader.GetOrdinal("DESHOR"));
                            objeto.TELCPAC = reader.GetString(reader.GetOrdinal("TELCPAC"));
                            objeto.TELHPAC = reader.GetString(reader.GetOrdinal("TELHPAC"));
                            objeto.SEXPAC = reader.GetString(reader.GetOrdinal("SEXPAC"));
                            return objeto;
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
        }


        public void restaurarCita(string CODCIT)
        {
            try
            {
                using (SqlConnection oCn = new SqlConnection(ConfigurationManager.ConnectionStrings["CNX"].ConnectionString.ToString()))
                {
                    using (SqlCommand oCmd = new SqlCommand("USP_RESTAURAR_CITA", oCn))
                    {
                        oCmd.Parameters.Add("@CODCIT", SqlDbType.VarChar, 20).Value = CODCIT;
                        oCmd.CommandType = CommandType.StoredProcedure;
                        oCmd.Connection = oCn;
                        oCn.Open();
                        oCmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception)
            {

            }
        }


        public void enviarDelivery(string CODCIT)
        {
            MessageQueue msMq = null;
            try
            {

                citaBE cita;
                using (SqlConnection oCn = new SqlConnection(ConfigurationManager.ConnectionStrings["CNX"].ConnectionString.ToString()))
                {
                    using (SqlCommand oCmd = new SqlCommand("USP_ENVIAR_DELIVERY", oCn))
                    {
                        oCmd.Parameters.Add("@CODCIT", SqlDbType.VarChar, 20).Value = CODCIT;
                        oCmd.CommandType = CommandType.StoredProcedure;
                        oCmd.Connection = oCn;
                        oCn.Open();
                        SqlDataReader oraRd = oCmd.ExecuteReader();
                        if (oraRd.HasRows)
                        {
                            oraRd.Read();
                            cita = new citaBE();
                            cita.CODCIT = oraRd.GetInt32(oraRd.GetOrdinal("CODCIT"));
                            cita.NOMPAC = oraRd.GetString(oraRd.GetOrdinal("NOMPAC"));
                            cita.APEPPAC = oraRd.GetString(oraRd.GetOrdinal("APEPPAC"));
                            cita.APEMPAC = oraRd.GetString(oraRd.GetOrdinal("APEMPAC"));
                            cita.DNIPAC = oraRd.GetString(oraRd.GetOrdinal("DNIPAC"));
                            cita.DIRPAC = oraRd.GetString(oraRd.GetOrdinal("DIRPAC"));
                            //cita.NOMDIS = oraRd.GetString(oraRd.GetOrdinal("NOMDIS"));
                            //cita.FECCITA = oraRd.GetString(oraRd.GetOrdinal("FECCITA"));
                            //cita.DESHOR = oraRd.GetString(oraRd.GetOrdinal("DESHOR"));
                            //cita.NOMEMP = oraRd.GetString(oraRd.GetOrdinal("NOMEMP"));
                            //cita.NOMOCU = oraRd.GetString(oraRd.GetOrdinal("NOMOCU"));
                            


                            msMq = new MessageQueue(".\\private$\\msgIN");
                            msMq.MessageReadPropertyFilter.Priority = true;
                            Message msg1 = new Message(cita);
                            msg1.Label = cita.DNIPAC;
                            msg1.Priority = MessagePriority.Normal;
                            msMq.Send(msg1);

                        }
                    }
                }
            }
            catch (MessageQueueException exMessage) {
                Console.Write(exMessage.ToString());
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            finally
            {
                msMq.Close();
            }
            Console.WriteLine("Message sent ......");
        }
    }
}
